﻿using System;
using System.Collections.Generic;
using Godot;
using kemocard.Scripts.Buff;
using kemocard.Scripts.Common;
using kemocard.Scripts.Enemy;
using kemocard.Scripts.Pawn;

namespace kemocard.Scripts.Module.Battle;

public sealed class BattleEnemy : BasePawn, IBattlePawn
{
    private int _currentHealth;
    private EnemyScript _script;

    int IBattlePawn.MaxHealth
    {
        get => MaxHealth;
        set => MaxHealth = value;
    }

    private int _maxHealth;

    public override int MaxHealth
    {
        get => _maxHealth;
        set
        {
            _maxHealth = value;
            if (_maxHealth < 1) _maxHealth = 1;
            if (_maxHealth < CurrentHealth)
            {
                CurrentHealth = _maxHealth;
            }
        }
    }

    public int CurrentHealth
    {
        get => _currentHealth;
        set => _currentHealth = Math.Max(0, value);
    }

    public bool IsDead { get; set; }

    int IBattlePawn.PDefense
    {
        get => PDefense;
        set => PDefense = value;
    }

    int IBattlePawn.MDefense
    {
        get => MDefense;
        set => MDefense = value;
    }

    public bool IsSilenced { get; set; }

    public BattleEnemy(BasePawn pawn)
    {
        InitFromConfig(pawn.Id);
        CurrentHealth = MaxHealth;
        Position = pawn.Position;
        // var path = StaticUtil.GetEnemyScriptPath(Id);
        // if (!FileAccess.FileExists(path)) return;
        // var res = ResourceLoader.Load<CSharpScript>(path);
        // _script = res?.New().As<EnemyScript>();
        _script = EnemyFactory.CreateEnemy(pawn.Id);
        _actionDesc = _script?.GetActionDesc() ?? new Dictionary<string, string>();
    }

    public string ActionDesc
    {
        get
        {
            string result = "";
            for (var index = 0; index < _actionId.Count; index++)
            {
                var id = _actionId[index];
                result += _actionDesc.GetValueOrDefault(id, "");
                if (index < _actionId.Count - 1) result += "\n";
            }

            return result;
        }
    }

    private List<string> _actionId = [];
    private Dictionary<string, string> _actionDesc;

    public void Action(PriorityQueue<EnemyAction, int> queue)
    {
        _script?.Action(this, _actionId, queue);
    }

    public string GetEnemyDesc()
    {
        string result = "";
        result +=
            $"{Name}\n生命:{CurrentHealth}/{MaxHealth}\n物理攻击:{PAttack}  魔法攻击:{MAttack}\n物理防御:{PDefense}  魔法防御:{MDefense}\n行为:{ActionDesc}";
        return result;
    }

    public void UpdateActionId()
    {
        _actionId = _script?.UpdateActionId() ?? [];
    }


    public override void Dispose()
    {
        _script = null;
        GameCore.EventBus.RemoveObjAllEvents(this);
        base.Dispose();
    }

    public void Dead()
    {
        // 这里只是让编译器开心，实际不会传任何参数
        var data = new object();
        ExecuteBuffs(ref data, BuffTag.BeforeDead);
        IsDead = true;
        CurrentHealth = 0;
    }

    public float GetReduction() => Reduction;
}

public class EnemyAction
{
    public int Priority;
    public Action<EnemyAction> Action;
    public BattleEnemy User;
    public List<BasePawn> Target;
}